package cz.acies.face.viewers;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

import cz.acies.data.dao.MModelFolderDAO;
import cz.acies.data.persist.TreeNode;

public class TreeViewer extends Viewer
{
	private static final long serialVersionUID = -1956904434547156256L;

	protected String id;
	protected List<TreeNode> items;
	
	public TreeViewer() {
		this.id = "treePanel";
	}
	
	@Override
	public void createPartControl() {
		
	}
	
	@Override
	public Object getInput() {
		return items = new ArrayList<TreeNode>();
	}
	
	@Override
	public void setInput(Object input) {
		try {
			if (!(input instanceof List)) throw new Exception("Input object must by List<TreeNode> Type.");
			else {
				this.items = (List<TreeNode>)input; 
			}
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	@Override
	public String toHtml() {
		Gson gson = new Gson();
		String html = 
		"<div id=\""+this.id+"\" data-options=\"region:'west'\" style=\"width:200px; padding-top: 10px;\">" + 
			"<ul id=\"folderTree\" class=\"easyui-tree\"></ul>" + 
			"<script type=\"text/javascript\">" + 
				"$(\"#"+this.id+"\").tree({" + 
					"data: "+gson.toJson(getInput())+"," + 
					"onContextMenu: function(e, node){" + 
						"e.preventDefault();" + 
						"$(\"#"+this.id+"\").tree(\"select\", node.target);" + 
						"$(\"#foldersCtxMenu\").menu(\"show\", {" + 
							"left: e.pageX," + 
							"top: e.pageY" + 
						"});" + 
					"}," + 
				"onSelect: function(node){" + 
					"if (node.attributes.selectable) {" + 
						 
					"}" + 
				"}" + 
			"});" + 
			"</script>" + 
		"</div>"; 

		return html;
	}


}
